/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 沈小白 2020-07-22 $
 */<template> 
	<view class="container">
		<view class="user-container">
			<view class="contbox">
				<view @tap="changeAvatar" class="user-head">
					<view class="user-txt">头像</view>
					<view class="txs_fix">
						<image class="user_headimg" mode="aspectFill" :src="userInfo.head_pic?userInfo.head_pic:defaultImg"
						 role="img"></image>
					</view>
				</view>
				<view @tap="editUserInfo" class="user-name" data-type="nickname">
					<view class="user-name-txt">昵称</view>
					<image class="arrow-right" :src="imgUrl+'common/icon-arrowdown.png'" role="img"></image>
					<view class="user-txt-right one-line">{{userInfo.nickname}}</view>
				</view>
				<view @tap="editUserInfo" class="user-name" data-type="sex">
					<view class="user-name-txt">性别</view>
					<image class="arrow-right" :src="imgUrl+'common/icon-arrowdown.png'" role="img"></image>
					<view class="user-txt-right">{{userInfo.sex==0?'保密':userInfo.sex==1?'男':'女'}}</view>
				</view>
				<view @tap="editUserInfo" class="user-name" data-type="mobile">
					<view class="user-name-txt">手机</view>
					<image class="arrow-right" :src="imgUrl+'common/icon-arrowdown.png'" role="img"></image>
					<view class="user-txt-right">{{userInfo.mobile}}</view>
				</view>
				<view @tap="editUserInfo" class="user-name" data-type="email">
					<view class="user-name-txt">邮箱</view>
					<image class="arrow-right" :src="imgUrl+'common/icon-arrowdown.png'" role="img"></image>
					<view class="user-txt-right one-line">{{userInfo.email}}</view>
				</view>
			</view>
			<view class="contbox">
				<view @tap="editUserInfo" class="user-change" data-type="password">
					<view class="user-name-txt">修改密码</view>
					<image class="arrow-right" :src="imgUrl+'common/icon-arrowdown.png'" role="img"></image>
				</view>
				<view @tap="editUserInfo" class="user-name" data-type="paypwd">
					<view class="user-name-txt">支付密码</view>
					<image class="arrow-right" :src="imgUrl+'common/icon-arrowdown.png'" role="img"></image>
				</view>
				<navigator class="user-name" url="/pages/user/address_list/address_list" role="navigation">
					<view class="user-name-txt">收货地址</view>
					<image class="arrow-right" :src="imgUrl+'common/icon-arrowdown.png'" role="img"></image>
				</navigator>
			</view>
		</view>
		<block v-if="hasLogin">
			<view class="exitLogin" @tap="exit">安全退出</view>
		</block>
	</view>
</template>

<script>
	import {putUser,loginOut} from "../../../api/api.js";
	import config from "@/api/config.js";
	import common from "@/common/common.js";
	import {mapActions,mapState} from 'vuex';
	const globalData=getApp().globalData//从app.vue获取用户信息
	export default {
		data() {
			return {
				imgUrl:config.imgUrl,
				hostUrl:config.host,//路径前缀
				defaultImg:config.imgUrl+'common/user68.jpg',
				userInfo:"",//用户信息
				
			};
		},
		computed: mapState(['hasLogin','user']),
		onLoad(){
			if(this.hasLogin){
				this.userInfo=this.user;
			}else{
				
			}
			
		},
		onShow(){
			
			
		},
		methods:{
			...mapActions(['logout','getUser']),
			putUser(params){
				putUser(params).then(res=>{
					console.log(res)
					if (res.status==1) {
						common.toast("none", "修改成功", 2000)
						this.getUser()
					}else{
						common.toast("none", res.msg, 2000)
					}
				})
			},
			/*点击编辑*/
			editUserInfo(e){
				console.log(e)
				let type=e.currentTarget.dataset.type;
				uni.navigateTo({
					url:`/pages/user/userinfo_edit/userinfo_edit?type=${type}`
				})
			},
			/*设置头像*/ 
			changeAvatar(){
				var that =this;
				uni.chooseImage({
					count: 1, //最多1张图片,默认9
					sizeType: ['compressed', 'original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera', 'album'], //可以指定来源是相册还是相机，默认二者都有
					success: function (res) {
						// console.log(res)
						uni.uploadFile({
							url:'/api' + '/mall/upload',
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								"type":"head_pic",
							},
							
							success: function (res) {
								console.log(JSON.parse(res.data).result)
								console.log(res)
								that.userInfo.head_pic=JSON.parse(res.data).result
								let params={
									"head_pic":JSON.parse(res.data).result
								}
								
								that.putUser(params)
								
							}
						});
					},
					complete:(()=>{
						setTimeout(()=>{
							that.userInfo=that.user
						},500)
					})
				});
			},
			exit(){
				
				this.logout()
				
				
				// sessionStorage.clear();
				// this.loginOut();
				
			},
		}
	}
</script>

<style lang="scss">
	.user-container {
		font-size: 30rpx;
	}

	.user-head {
		width: 100%;
		height: 100rpx;
		position: relative;
		background-color: white;
	}

	.user-txt {
		float: left;
		margin-left: 40rpx;
		margin-top: 30rpx;
	}

	.txs_fix {
		height: 80rpx;
		width: 80rpx;
		overflow: hidden;
		border-radius: 40rpx;
		float: right;
		margin-right: 30rpx;
		margin-top: 10rpx;
	}

	.txs_fix .user_headimg {
		width: 100%;
		height: 100%;
	}

	.user-name {
		width: 100%;
		height: 100rpx;
		margin-top: 2rpx;
		background-color: white;
	}

	.user-name-txt {
		float: left;
		margin-left: 40rpx;
		margin-top: 30rpx;
	}

	.user-txt-right {
		float: right;
		margin-top: 30rpx;
		margin-right: 30rpx;
	}

	.user-change {
		width: 100%;
		height: 100rpx;
		background-color: white;
	}

	.one-line {
		white-space: nowrap;
		word-break: keep-all;
		overflow: hidden;
	}

	.arrow-right {
		width: 30rpx;
		height: 30rpx;
		margin-right: 30rpx;
		float: right;
		margin-top: 30rpx;
	}

	.contbox {
		margin: 0 auto;
		margin-top: 20rpx;
		width: 710rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}
	.exitLogin{
		margin: 20rpx 20rpx 0 20rpx;
		height: 90rpx;
		background: #ff3b3b;
		color: #FFFFFF;
		text-align: center;
		line-height: 90rpx;
		font-size: 32rpx;
	}
</style>
